// package-market/components/lottery-type-flip/index.js
import componentBehaviors from '../../../behaviors/component';
import { COMPONENT_OPTIONS } from '../../../constants/common';
import { PACKAGE_MARKET_PAGE } from '../../../constants/route';
import { navTo } from '../../../common/env';
import { isEmpty } from '../../../common/utils';
import { dealFlipLotteryAwardData, getOneEmptyValue } from '../../biztools/lottery';

Component({
    behaviors: [componentBehaviors],
    options: {
        ...COMPONENT_OPTIONS,
    },
    properties: {
        // 活动id
        activityId: {
            type: String,
            value: '',
        },
        // 通知列表
        noticeList: {
            type: Array,
            value: [],
        },
        // 奖品列表
        awardList: {
            type: Array,
            value: [],
        },
        // 抽奖机会
        count: {
            type: Number,
            value: 0,
        },
        // 抽奖结果
        result: {
            type: Object,
            value: {},
        },
    },
    observers: {
        'result': function (result) {
            if (result && !isEmpty(result)) {
                const { prize } = result;
                const { awardList } = this.data;
                let { award } = getOneEmptyValue();
                if (prize > 0) {
                    award = dealFlipLotteryAwardData(awardList, prize);
                }
                this.setData({
                    cardStyle: 'transform: rotateY(-180deg);',
                    activeValue: result.activeValue,
                    lotteryResult: {
                        isEmpty: prize > 0 ? false : true, // 是否中奖
                        isStart: false,
                        award, // 奖品信息,
                    },
                }, () => {
                    setTimeout(() => {
                        this.setData({
                            showResult: true,
                        });
                    }, 1000)
                });
            }
        },
    },
    data: {
        showList: [1, 2, 3, 4, 5, 6, 7, 8, 9],
        cardStyle: '',
        activeValue: 0,
        showResult: false,
        lotteryResult: {},
    },
    methods: {
        // 开始抽奖
        handleStart(e) {
            const { value } = e.currentTarget.dataset;
            this.triggerEvent('start', { activeValue: value });
        },
        // 重置
        handleReset() {
            this.setData({
                cardStyle: '',
                activeValue: 0,
                showResult: false,
                lotteryResult: {},
            });
        },
        // 关闭
        handleClose(e) {
            if (e.detail) {
                const { activityId } = this.data;
                navTo(PACKAGE_MARKET_PAGE.lotteryRecord, {
                    activityId,
                });
            } else {
                this.handleReset();
            }
        },
        // 分享
        handleShare() {
            this.triggerEvent('share');
        },
    }
})
